<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

use App\Models\StaffInfo;
use App\Services\SendMobileMsgServices;

class StaffSms extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'staff:sms';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '员工批量发短信';

    protected $mobile;

    /**
     * Create a new command instance.
     *
     * @return void
     */
    function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $staffs = StaffInfo::where('autherized', 0)
                            ->orderBy('id', 'desc')
                            ->get();
        // send sms
        $sms = new SendMobileMsgServices;

        $invalidMobiles = [];

        $n = 0;
        foreach ($staffs as $staff) {
            if (!preg_match('/^[1][3-9][0-9]{9}$/', $staff->mobile, $matches)) {
                $invalidMobiles[] = sprintf(
                                '[%s] - [%s] - [%s] - [%s][%d]', 
                                $staff->id, 
                                $staff->name, 
                                $staff->idno, 
                                $staff->mobile, 
                                strlen($staff->mobile)
                            );
            }
            $sms->send($staff->mobile, '12月31日', 'ygc_05');
            $this->info(sprintf('STAFF_SMS: %05d. %s %s', ++$n, $staff->id, $staff->mobile));
        }

        print_r($invalidMobiles);
        @file_put_contents('invalid_mobiles.txt', implode(PHP_EOL, $invalidMobiles));
    }

}
